package com.fjnu.dao.impl;

import com.fjnu.aspect.NPECatch;
import com.fjnu.dao.UserDao;
import com.fjnu.entity.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.*;

/**
 * @desc:
 * @author:
 * @date:
 */
@Component
public class UserDaoImpl implements UserDao {
    /**
     * 注入 connection Bean,利用connection bean来完成对数据库的操作
     */

    @Autowired
    private Connection connection;

    @Override
    @NPECatch
    public int insert(User user) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into " +
                "user(user_id,username,password,nickname,email,phone,sex,age,img,status,create_time,update_time)" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?)");
        preparedStatement.setString(1, user.getUser_id());
        preparedStatement.setString(2, user.getUsername());
        preparedStatement.setString(3, user.getPassword());
        preparedStatement.setString(4, user.getNickname());
        preparedStatement.setString(5, user.getEmail());
        preparedStatement.setString(6, user.getPhone());
        preparedStatement.setInt(7, user.getSex());
        preparedStatement.setInt(8, user.getAge());
        preparedStatement.setString(9, user.getImg());
        preparedStatement.setInt(10, user.getStatus());
        preparedStatement.setString(11, user.getCreate_time());
        preparedStatement.setString(12, user.getUpdate_time());
        return preparedStatement.executeUpdate();
    }

    @Override
    public int delete(String userId) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("delete from user where user_id = ?");
        preparedStatement.setString(1, userId);
        return preparedStatement.executeUpdate();
    }

    @Override
    public int updateById(String userId, User newUser) throws SQLException {
        System.out.println("updateById");
        PreparedStatement preparedStatement = connection.prepareStatement("update user set username = ?,password = ?," +
                "nickname = ?,email = ?,phone = ?,sex = ?" +
                ",age = ?,img = ?,status = ?,update_time = ? where user_id = ?");
        preparedStatement.setString(1, newUser.getUsername());
        preparedStatement.setString(2, newUser.getPassword());
        preparedStatement.setString(3, newUser.getNickname());
        preparedStatement.setString(4, newUser.getEmail());
        preparedStatement.setString(5, newUser.getPhone());
        preparedStatement.setInt(6, newUser.getSex());
        preparedStatement.setInt(7, newUser.getAge());
        preparedStatement.setString(8, newUser.getImg());
        preparedStatement.setInt(9, newUser.getStatus());
        preparedStatement.setString(10, newUser.getUpdate_time());
        preparedStatement.setString(11, userId);
        return preparedStatement.executeUpdate();
    }

    @Override
    public int updatePasswordById(String userId, String oldPassword, String newPassword) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement
                ("update user set password = ? where user_id = ? and password = ?");
        preparedStatement.setString(1, newPassword);
        preparedStatement.setString(2, userId);
        preparedStatement.setString(3, oldPassword);
        return preparedStatement.executeUpdate();
    }

    @Override
    public User selectById(String userId) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("select * from user where user_id = ?");
        preparedStatement.setString(1, userId);
        ResultSet resultSet = preparedStatement.executeQuery();
        User user = new User();
        while (resultSet.next()) {
            user.setUser_id(resultSet.getString("user_id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setNickname(resultSet.getString("nickname"));
            user.setEmail(resultSet.getString("email"));
            user.setPhone(resultSet.getString("phone"));
            user.setSex(resultSet.getInt("sex"));
            user.setAge(resultSet.getInt("age"));
            user.setImg(resultSet.getString("img"));
            user.setStatus(resultSet.getInt("status"));
            user.setCreate_time(resultSet.getString("create_time"));
            user.setUpdate_time(resultSet.getString("update_time"));
        }
        return user;
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("select * from user where username = ? and password = ?");
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();
        User user = new User();
        while (resultSet.next()) {
            user.setUser_id(resultSet.getString("user_id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setNickname(resultSet.getString("nickname"));
            user.setEmail(resultSet.getString("email"));
            user.setPhone(resultSet.getString("phone"));
            user.setSex(resultSet.getInt("sex"));
            user.setAge(resultSet.getInt("age"));
            user.setImg(resultSet.getString("img"));
            user.setStatus(resultSet.getInt("status"));
            user.setCreate_time(resultSet.getString("create_time"));
            user.setUpdate_time(resultSet.getString("update_time"));
        }

        return user;
    }

    @Override
    public int selectCount() throws SQLException {
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select count(*) from user");
        resultSet.next();
        return resultSet.getInt(1);
    }
    @NPECatch
    public void testNOP() throws SQLException {
        throw new SQLException();
    }
}
